package com.drug.lib.util;

import android.text.TextUtils;

/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * <p>
 * 创建日期：2022/6/30 or 13:11
 * 描述：
 */
public class SPManager {

    private static final String TOKEN = "token";
    private static final String TOKEN_KEY = "token_key";
    private static final String REMEMBER_PWD = "rememberPassword";
    private static final String USER_ACCOUNT_KEY = "account_info";
    private static final String USER_PASSWORD_KEY = "password_info";

    public static String getToken() {
        return SpUtils.decodeString(TOKEN);
    }

    public static void setToken(String token) {
        SpUtils.encode(TOKEN, token);
    }

    public static String getTokenKey() {
        return SpUtils.decodeString(TOKEN_KEY);
    }

    public static void setTokenKey(String key) {
        SpUtils.encode(TOKEN_KEY, key);
    }

    public static String getUserName() {
        return SpUtils.decodeString(USER_ACCOUNT_KEY);
    }

    public static String getPassWord() {
        String encrypt = SpUtils.decodeString(USER_PASSWORD_KEY);
        DLog.d("加密字符串：" + encrypt);
        if (TextUtils.isEmpty(encrypt))
            return null;
        try {
            String pwd = KaisaUtil.decryptKaiser(encrypt, 30);
            DLog.d("解密后：" + pwd);
            return pwd;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static void putLoginInfo(String name, String pwd) {
        try {
            if (getRememberPassword()) {
                String encrypt = KaisaUtil.encryptKaisa(pwd, 30);
                DLog.d("加密后：" + encrypt);
                SpUtils.encode(USER_PASSWORD_KEY, encrypt);
            }
            SpUtils.encode(USER_ACCOUNT_KEY, name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static void putRememberPassword(Boolean isRemember) {
        SpUtils.encode(REMEMBER_PWD, isRemember);
        if (!isRemember) removePassword();
    }

    public static boolean getRememberPassword() {
        return SpUtils.decodeBoolean(REMEMBER_PWD);
    }

    public static void removePassword() {
        SpUtils.removeKey(USER_PASSWORD_KEY);
    }

    public static void removeACCOUNTMMKV() {
        SpUtils.clearAll();
    }
}
